﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
// Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
using System;

namespace ForgeCraft
{
    public class CmdTp : Command
    {
        public override string name { get { return "tp"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "op"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdTp() { }

        public override void Use(Player p, string message)
        {
            string name = message;
            if (name != null)
            {
                Player target = Server.FindPlayer(name);
                if (target != null)
                {
                    Position pos = target.pos;
                    pos.y += 2;
                    p.Send(PacketWriter.MakePositionAndLook(pos));
                    p.ChunkAreaPlayer = 8;
                    //p.UpdateChunks();
                }
                else
                {
                    int x, y, z;
                    if (int.TryParse(message.Split(' ')[0], out x) && int.TryParse(message.Split(' ')[1], out y) && int.TryParse(message.Split(' ')[2], out z))
                    {
                        if (y < 0 || y > 128)
                        {
                            p.Message(Color.Announcement + "Invalid Y coord");
                            return;
                        }

                        p.ChunkAreaPlayer = 7;
                        p.Send(PacketWriter.MakePositionAndLook(new Position((double)x, (double)y, (double)z)));
                        //p.UpdateChunks();
                        
                    }
                    else
                        p.NoPlayerMessage(name);
                }
            }
        }
        public override void Help(Player p)
        {
            p.Message(Color.Aqua + "/tp [PlayerName / X Y Z]");
            p.Message(Color.Help + "Teleports you to a specified player's location, or the coordinates your specify.");
        }
    }
}